import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class VideoDetailsWidget extends StatefulWidget {
  // final StreamController streamController;
  String videoUrl;
  int id;
  VideoDetailsWidget(this.videoUrl, this.id);
  // VideoDetailsWidget(this.videoUrl, this.id);

  @override
  _VideoDetails2WidgetState createState() =>
      _VideoDetails2WidgetState(videoUrl, id);
}

class _VideoDetails2WidgetState extends State<VideoDetailsWidget> {
  String videoUrl;
  int id;
  _VideoDetails2WidgetState(this.videoUrl, this.id);
  //创建视频播放控制器
  VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(videoUrl)
      ..initialize().then((_) {
        setState(() {});
      });
    _controller.setLooping(true);
  }

  @override
  void dispose() {
    _controller.dispose();
    print('是不是执行了这个');
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Hero(
        tag: this.id,
        child: Stack(
          alignment: Alignment.center,
          children: [
            //第一层的视频
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                AspectRatio(
                  aspectRatio: _controller.value.aspectRatio,
                  child: VideoPlayer(_controller),
                ),
              ],
            ),
            //第二层的控制按钮
            buildControllerWidget()
          ],
        ));
  }

  Widget buildControllerWidget() {
    return Positioned.fill(
      child: InkWell(
        child: Container(
          //0.3 的蓝色透明度
          color: Colors.blueGrey.withOpacity(0.5),
          //图标
          child: Icon(
            Icons.play_circle_fill,
            size: 150.sp,
            color: Colors.black.withOpacity(0.7),
          ),
        ),
        onTap: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (_) => Lookvideo(_controller, id),
              // settings: RouteSettings(isInitialRoute: true),
              fullscreenDialog: true,
            ),
          );
          print('点击了');
        },
      ),
    );
  }
}

class Lookvideo extends StatefulWidget {
  VideoPlayerController _controller;
  int id;
  Lookvideo(this._controller, this.id);
  _LookvideoState createState() => _LookvideoState(_controller, this.id);
}

class _LookvideoState extends State<Lookvideo> {
  VideoPlayerController _controller;
  int id;
  _LookvideoState(this._controller, this.id);
  @override
  @override
  void initState() {
    super.initState();
    _controller.play();
  }

  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: AppBar(title: Text('NewPage')),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: GestureDetector(
          child: Hero(
            tag: this.id,
            child: AspectRatio(
              aspectRatio: _controller.value.aspectRatio,
              child: VideoPlayer(_controller),
            ),
          ),
          onTap: () {
            this._controller.pause();
            Navigator.pop(context);
          },
        ),
      ),
    );
  }
}
